package LandR.register.phone.phoneSrc.l7;


import LandR.personal.personalHelp.function_jsp.JspPageUseMethod;
import LandR.register.phone.phoneSrc.function.User;
import LandR.register.phone.phoneSrc.function.jsonFunction;
import LandR.register.phone.phoneSrc.function.registerHelpFunction;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;

@WebServlet("/vc7")
public class ValiCode extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.得到数据
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        InputStreamReader insr = new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8);
        StringBuilder results = new StringBuilder();
        int respInt = insr.read();
        while (respInt != -1) { // 读取请求数据
            results.append((char) respInt);
            respInt = insr.read();
        }
        System.out.println("前端传递的手机号注册数据为：" + results.toString());
        jsonFunction jf = new jsonFunction();
        User user = jf.jsonToJavaObjectByFastJson(results.toString());

        System.out.println("开始手机号后台注册处理");

        String phoneNumber = user.getPhoneNumber();
        //incode是输入到文本框中的验证码
        String inCode = user.getInCode();
        //Valicode是随机出验证码
        String valiCode = request.getSession().getAttribute("valiCode").toString().toLowerCase();
        System.out.println("获取处理数据库的session的id是：" + request.getSession().getId());

        registerHelpFunction lhf = new registerHelpFunction();
        JspPageUseMethod jpum = new JspPageUseMethod();
        //2.判断验证是否正确
        if (inCode.equals(valiCode)) {
            try {
                //判断这个用户是否已经存在
                if (!lhf.judegeUserFromUsersByPhoneNumber(user)) {
                    try {
                        //在users数据表中创建新用户并存入手机号
                        lhf.insertUserInUsers(user);
                        System.out.println("用户登录信息存入成功");
                        //在usersdata数据表中创建用户并存入手机号
                        LandR.personal.personalHelp.Users.User user1 = new LandR.personal.personalHelp.Users.User();
                        user1.setPhoneNumber(user.getPhoneNumber());
                        jpum.insertUserData(user1);
                        System.out.println("用户个人信息存入成功");
                        PrintWriter out = response.getWriter();
                        System.out.println("注册成功");
                        out.print("手机号注册成功！");


                    } catch (SQLException e) {
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }

                } else {
                    PrintWriter out = response.getWriter();
                    out.print("用户已存在，注册失败！");
                    System.out.println("用户已存在，注册失败！");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            PrintWriter out = response.getWriter();
            System.out.println("验证码错误，注册失败！");
            out.print("验证码错误，注册失败！");

        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
